//index.js
//获取应用实例
var app = getApp()
//引入js文件
var myIndex = require("../../data/index.js");
Page({
  data: {
    width: '650rpx',
    search: {
      searchValue: '',
      showClearBtn: false
    },
    latitude: '',
    longitude: '',
    // tab切换  
    currentNavtab: 0,
    district: [],
    districtIdx: 0,
    open: false,
    isfull: false,
    name: '',
    flag: true,
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    feed: [],
    feed_length: 0,
    typeList: [], //类型工具导航
    hotList: [], //热门推荐
    productList: [], //可租产品,
  },
  // 加载
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var ww = res.windowWidth;
        var wd = "560px";
        if (ww <= 320){
          wd = "270px";
        } else if (ww <= 360 && ww > 320) {
          wd = "310px";
        } else if (ww <= 375 && ww > 360){
          wd = "320px";
        } else if (ww <= 414 && ww > 375){
          wd = "356px";
        } else {
          wd = "400px";
        }
        that.setData({
          width: wd,
        });
      },
    })
    myIndex.village_LBS(that);
    that.setData({
      district: myIndex.init_district(that),
      imgUrls: myIndex.banner_list(),
      name: myIndex.init_district(that)[0],
      typeList: myIndex.init_type(),
      hotList: myIndex.host_data.data,
      productList: myIndex.product_data.data,
    });
  },
  //输入内容时
  searchActiveChangeinput: function (e) {
    const val = e.detail.value;
    console.log(val);
    this.setData({
      'search.showClearBtn': val != '' ? true : false,
      'search.searchValue': val
    })
  },
  //点击清除搜索内容
  searchActiveChangeclear: function (e) {
    this.setData({
      'search.showClearBtn': false,
      'search.searchValue': ''
    })
  },
  //点击聚集时
  focusSearch: function () {
    if (this.data.search.searchValue) {
      this.setData({
        'search.showClearBtn': true
      })
    }
  },
  //搜索框
  searchSubmit: function () {
    const val = this.data.search.searchValue;
    console.log("searchSubmit >> " + val);
    if (val) {
      const that = this,
        app = getApp();
      wx.showToast({
        title: '搜索中',
        icon: 'loading'
      });   
    }
  },
  //切换tab
  switchTab: function (e) {
    var that = this;
    that.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
  },
  //显示
  openDistrict: function (e) {
    var that = this;
    that.setData({
      open: true,
      isfull: true,
      district: myIndex.init_district(that),
    });
  },
  //选择小区
  switchDistrict: function (e) {
    var that = this;
    that.setData({
      isfull: false,
      open: false,
      districtIdx: e.currentTarget.dataset.dix,
      name: myIndex.init_district(that)[e.currentTarget.dataset.idx],
    });
  },
  //banner
  bindBannerView: function (e) {
    var id = e.currentTarget.dataset.key;
    console.log(id);
  },
  //详情
  detail: function (e) {
    var id = e.currentTarget.dataset.key;
    console.log(id);
    wx.navigateTo({
      url: '../detail/detail?pid='+id,
    })
  },
  //种类列表信息
  bindTypeList: function (e) {
    var id = e.currentTarget.dataset.key;
    console.log(id);
  },
  //上拉加载可出租产品
  lowerMore: function () {
    console.log("lowerMore");
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      that.nextLoad();
    }, 1000);
  },
  //刷新
  nextLoad: function (e) {
    var that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 4000
    })
    var next = myIndex.product_data;
    console.log("continueload");
    var next_data = next.data;
    console.log(next_data);
    that.setData({
      productList: that.data.productList.concat(next_data)
    });
    setTimeout(function () {
      wx.showToast({
        title: '加载成功',
        icon: 'success',
        duration: 2000
      })
    }, 3000);
  },
  //加载
  onReady: function () {
    var that = this;
    wx.getSetting({
      success: (res) => {
        console.log(res);
        console.log(res.authSetting['scope.userLocation']);
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (res) {
              if (res.cancel) {
                console.info("授权失败返回数据");
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (data) {
                    console.log(data);
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API
                      village_LBS(that);
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
          village_LBS(that);
        }
      }
    })
  }
})
